import os
import random
import factory
from PIL import Image

from project.config import settings
from project.database import SessionLocal
from project.tdd.models import Member


class MemberFactory(factory.alchemy.SQLAlchemyModelFactory):
    class Meta:
        model = Member
        sqlalchemy_session = SessionLocal()
        sqlalchemy_get_or_create = ('username',)
        sqlalchemy_session_persistence = 'commit'

    username = factory.Faker('user_name')
    email = factory.LazyAttribute(lambda o: '%s@example.com' % o.username)

    @factory.lazy_attribute
    def avatar(self):
        width = 300
        height = 300
        color = random.choice(('red', 'green', 'blue', 'violet', 'yellow'))
        image_format = 'JPEG'
        image_palette = 'RGB'

        with Image.new(image_palette, (width, height), color) as thumb:
            filename = f'{self.username}.jpg'
            full_path = os.path.join(
                settings.UPLOADS_DEFAULT_DEST,
                filename
            )
            thumb.save(full_path, format=image_format)

        return filename
